package leader.service;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

import beanDao.MapToBean;

import teacher.bean.Award;
import teacher.bean.Paper;
import teacher.bean.Teacher;
import teacher.bean.Treatise;
import tools.StringDate;
import database.dao.MysqlCrud;

public class DirectAwardService {
	/**
	 * @category 查询同一方向所有的获奖
	 * @author chenkai
	 * @param teacher Teacher 方向信息
	 * @return list List<Award> 返回一个List<Award>获奖集合
	 */
	public List<Award> selectAllDirectAward(Teacher teacher) {
		List<Award> lists = new ArrayList<Award>();
		String sql="select * from (select * from award order by state,kind,level,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一方向所有的获奖  分页
	 * @author chenkai
	 * @param teacher Teacher 方向信息
	 * @return list List<Award> 返回一个List<Award>获奖集合
	 */
	public List<Award> selectSegregateAllDirectAward(Teacher teacher,int pageNow,int pageSize) {
		List<Award> lists = new ArrayList<Award>();
		String sql="select * from (select * from award order by state,kind,level,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>? and awaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的获奖
	 * @param vague 查询的获奖的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Treatise> 返回一个List<Treatise>的获奖集合
	 */
	public List<Award> selectAwardLikeName(String vague,Teacher leader) {
		String sql= "select * from (select * from award order by state,kind,level,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Award> lists=new ArrayList<Award>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过名称模糊查询一个研究方向的获奖  分页
	 * @param vague 查询的获奖的名称
	 * @param leader 传入的研究方向
	 * @return treatiseOne List<Treatise> 返回一个List<Treatise>的获奖集合
	 */
	public List<Award> selectSegregateAwardLikeName(String vague,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from award order by state,kind,level,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.state<>? and awaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Award> lists=new ArrayList<Award>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql,parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向论著论著
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return treatiseOne List<Treatise> 返回查询到的List<Treatise>论著列表
	 */
	public List<Award> selectDirectAwardByTime(Date date1,Date date2,Teacher leader) {
		String sql= "select * from (select * from award order by state,kind,level,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Award> lists=new ArrayList<Award>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 通过时间段查询方向论著论著  分页
	 * @param date1 时间段的下限
	 * @param date2 时间段的上限
	 * @param leader 查询的方向
	 * @return treatiseOne List<Treatise> 返回查询到的List<Treatise>论著列表
	 */
	public List<Award> selectSegregateDirectAwardByTime(Date date1,Date date2,Teacher leader,int pageNow,int pageSize) {
		String sql= "select * from (select * from award order by state,kind,level,date desc) as acheve,teacher as T where acheve.teaId=T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>? and awaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(leader.getDirection()),
				String.valueOf(1)
				};
		List<Award> lists=new ArrayList<Award>();
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list = mysqlcurd.SelectData(sql, parameter);
		//System.out.println(list.size());
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 查询同一方向所有未审核的获奖  分页
	 * @author chenkai
	 * @param teacher Teacher 方向信息
	 * @return list List<Award> 返回一个List<Award>获奖集合
	 */
	public List<Award> selectSegregateDirectAwardByState(Teacher teacher,Award awa,int pageNow,int pageSize) {
		List<Award> lists = new ArrayList<Award>();
		String sql="select * from (select * from award order by state,kind,level,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state=? and T.direction=? and acheve.state<>? and awaId LIMIT " +
				""+(pageNow-1)+" ,"+pageSize+"";
		String[] parameter={
				String.valueOf(awa.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}

	/**
	 * @category 查询同一方向所有未审核的获奖  分页
	 * @author chenkai
	 * @param teacher Teacher 方向信息
	 * @return list List<Award> 返回一个List<Award>获奖集合
	 */
	public List<Award> selectDirectAwardByState(Teacher teacher,Award awa) {
		List<Award> lists = new ArrayList<Award>();
		String sql="select * from (select * from award order by state,kind,level,date desc) as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state=? and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(awa.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> list= mysqlcurd.SelectData(sql, parameter);
		for(int i=0;i<list.size();i++) {
			Map map=list.get(i);
			lists.add(new MapToBean().MapToAward(map));
		}
		return lists;
	}
	
	/**
	 * @category 获奖的审核
	 * @author chenkai
	 * @param paper Paper 传入审核的获奖以及审核结果
	 * @return flag boolean
	 */
	public boolean updateAwardState(Award award) {
		boolean flag = false;
		String sql="update award set state = ? where awaId = ? ";
		String[] parameter={
				String.valueOf(award.getState()),
				String.valueOf(award.getAwaId())
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		flag = mysqlcurd.InsertAndUpdateData(sql, parameter);
		return flag;
	}
	
	/**
	 * @category 得到一个方向的所有获奖的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectAward(Teacher teacher) {
		int sum=0;
		String sql="select count(*) from award as acheve ,teacher as T where acheve.teaId = T.teaId and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的时间段获奖的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectAwardLikeName(Teacher teacher,String vague) {
		int sum=0;
		String sql="select count(*) from award as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.name like '%"+vague+"%' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有获奖的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfTimeDirectAward(Teacher teacher,Date date1,Date date2) {
		int sum=0;
		String sql="select count(*) from award as acheve ,teacher as T where acheve.teaId = T.teaId and date>='"+date1+"' and date<='"+date2+"' and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
	
	/**
	 * @category 得到一个方向的所有获奖的数量
	 * @author chenkai
	 * @return sum int 教师数量
	 */
	public int getCountOfDirectAwardByState(Teacher teacher,Award award) {
		int sum=0;
		String sql="select count(*) from award as acheve ,teacher as T where acheve.teaId = T.teaId and acheve.state = ? and T.direction=? and acheve.state<>?";
		String[] parameter={
				String.valueOf(award.getState()),
				String.valueOf(teacher.getDirection()),
				String.valueOf(1)
				};
		MysqlCrud mysqlcurd=new MysqlCrud();
		List<Map> lists=mysqlcurd.SelectData(sql,parameter);
		Map map=lists.get(0);
		sum=Integer.parseInt(map.get(0).toString());
		return sum;
	}
}
